home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 11807 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.4 KB  |  96 lines

  1. Path: lrz-muenchen.de!news
  2. From: watzka@stat.uni-muenchen.de (Kurt Watzka)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Please help me
  5. Date: 26 Mar 1996 15:48:53 GMT
  6. Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
  7. Distribution: world
  8. Message-ID: <4j93l5$f1g@sparcserver.lrz-muenchen.de>
  9. References: <4j6nrl$lfk@badger.wmin.ac.uk>
  10. NNTP-Posting-Host: sun2.lrz-muenchen.de
  11.  
  12. darec@westminster.ac.uk (Nadarajah Thavaneethan) writes:
  13.  
  14. >#include <stdio.h>
  15. >#include <stdlib.h>
  16. >#define  SIZE 60
  17.  
  18. >void main(void)
  19.  
  20. I will not comment on this.
  21.  
  22. >{
  23. >        char c[SIZE];
  24. >int index, next;
  25. >    for (index=0; index(SIZE && ( c[index] = getchar() != '\n'); index++);
  26.  
  27. Is there a good reason not to use fgets()?
  28.   
  29. >     c[index] = 0;
  30. >         for(; index > 0; index--)
  31. >            for (next = 0; next < index; next++)
  32. >               compchar(c[next-1],c[next]);
  33.  
  34. It's probably not such a good idea to access "c[-1]". There are at least
  35. two reasons for this:
  36.  
  37.  1) Pointer arithmetic is not defined on memory that does not belong to
  38.     _one_ object.
  39.  
  40.  2) "c[-1]" might refer to memory that does not belong to your program.
  41.  
  42. >            printf("\n the line is now %s\n",c);
  43. > }
  44.  
  45. > void compchar (char c1, char c2)
  46. > {
  47.  
  48. > char p;
  49.  
  50.  
  51. >          if (c1 > c2)
  52. >              p = c1;
  53. >              c1 = c2;
  54. >              c2 = p;
  55. > }
  56.  
  57. C has _no_ call by reference. You have to use pointers to variables
  58. if you want to mimic "call by reference" in C. Since passing a
  59. pointer is what most programming languages that have call by reference
  60. do to implement it, this is _not_ a disadvantage, it just makes it
  61. more obvious to the programmer what is going on.
  62.  
  63. Indentation does not express structure in C, so, as far as your
  64. compiler is concerned, only the first assignment depends on the
  65. "if". 
  66.  
  67. So:
  68.  
  69.    void compchar(char *c1, char *c2)
  70.    {
  71.       char p;
  72.  
  73.       if (*c1 > *c2) {
  74.          p = *c1;
  75.          *c1 = *c2;
  76.          *c2 = p;
  77.       }
  78.    }
  79.  
  80. might be the conditional swap you are looking for.
  81.  
  82. >This program reads a line of data and sorts it into ascending ASCII sequence
  83. > it doesn't but it should.
  84.  
  85. Obviously, all the program should do is either print "42" or guess what
  86. you really wanted it to do and do exactly that, depending on the phase
  87. of the moon and the current price for pickeled herring in the netherlands. 
  88. The FAQ for comp.lang.c tells you why.
  89.  
  90. Kurt
  91. --
  92. | Kurt Watzka                             Phone : +49-89-2180-6254
  93. | watzka@stat.uni-muenchen.de
  94. | ua302aa@sunmail.lrz-muenchen.de
  95.  
  96.